package master;

import java.net.MalformedURLException;

/**
 * @author chenyw
 * @date 2022/9/5 16:05
 * 打家劫舍：环形数组
 */
public class M213rob2 {
    public static void main(String[] args) {
        int[] arr = {
                2,3,2
        };
        int rob = rob(arr);
        System.out.println(rob);
    }
    public static int rob(int[] nums) {
        int n = nums.length;
        if (n == 1) {
            return nums[0];
        }
        if (n == 2) {
            return Math.max(nums[0], nums[1]);
        }
        return Math.max(myRob(nums, 0, n - 2), myRob(nums, 1, n - 1));
    }

    private static int myRob(int[] nums, int start, int end) {
        int a = nums[start];
        int b = Math.max(nums[start], nums[start + 1]);
        for (int i = start + 2; i <= end; i++) {
            int tmp = Math.max(a + nums[i], b);
            a = b;
            b = tmp;
        }
        return b;
    }
}
